package com.http;

import java.util.ArrayList;

import org.json.JSONArray;

import android.os.AsyncTask;

import com.android.ocean.util.net.ErrorCode;
import com.android.ocean.util.net.NetConUtil;
import com.android.ocean.util.net.ResEntity;
import com.common.method.CommonData;
import com.common.method.ServerPath;
import com.modle.ChatMsgStruct;
import com.util.JsonUtil;

/**
 * 
 * @ClassName:ChatMsgListTask
 * @Description:TODO 
 * @author zhuw
 * @date 2013-8-19 上午11:36:48
 */
public class ChatMsgListTask extends AsyncTask<String, Void, ResultChatMsgList> {
	public static interface CallBack extends NetWorkCallBack {
		/**
		 * 
		 * @Description:TODO获取消息列表
		 * @param@param reqFlag
		 * @param@param dataList
		 * @return void
		 * @author zhuw
		 * @date 2013-8-19 下午11:01:10
		 */
		public abstract void procChatMsgListData(String reqFlag,ArrayList<ChatMsgStruct> dataList);
		/**
		 * 
		 * @Description:TODO获取消息失败
		 * @param@param reqFlag
		 * @param@param msg
		 * @return void
		 * @author zhuw
		 * @date 2013-8-19 下午11:00:33
		 */
		public abstract void procChatMsgListFailed(String reqFlag,String msg);

	}

	CallBack callback;
	String reqFlag;
	String hisThumb;//对方头像
	String myThumb;//我的头像
	public ChatMsgListTask(String reqFlag,CallBack callback,String hisThumb,String myThumb) {
		super();
		this.reqFlag=reqFlag;
		this.callback = callback;
		this.hisThumb = hisThumb;
		this.myThumb = myThumb;
	}

	@Override
	protected ResultChatMsgList doInBackground(String... params) {
		ResultChatMsgList data = null;
		try {
			ResEntity re=NetConUtil.sendGetRequestEntity(ServerPath.getChatPath(params[0], params[1],params[2], params[3],params[4]));
			if(re.code==ResEntity.S_OK){
			    data= new ResultChatMsgList(CommonData.H_FLAG_SUCCESS, null, JsonUtil.parseChatMsgList(new JSONArray(re.jsonStr),params[0],hisThumb,myThumb));
			}else if(re.code==ResEntity.S_FAILED){
				
				  data= new ResultChatMsgList(CommonData.H_FLAG_ERROR, "code:"+re.code+"    "+re.jsonStr, null);
				
			}else{
				  data= new ResultChatMsgList(CommonData.H_FLAG_ERROR, "code:"+re.code+ErrorCode.ERROR_MSG, null);
			}
		} catch (Exception e) {
			data= new ResultChatMsgList(CommonData.H_FLAG_ERROR, ErrorCode.ERROR_MSG, null);
			e.printStackTrace();
		}
		return data;
	}

	@Override
	protected void onPostExecute(ResultChatMsgList result) {
		if (result.flag == CommonData.H_FLAG_SUCCESS) {
			callback.procChatMsgListData(reqFlag,result.dataList);

		} else {
			callback.procChatMsgListFailed(reqFlag,result.msg);
		}

	}
}
